package com.ruiyian.project.log.controller;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.ruiyian.common.core.controller.BaseController;
import com.ruiyian.common.core.domain.AjaxResult;
import com.ruiyian.common.core.page.TableDataInfo;
import com.ruiyian.common.vo.LineVo;
import com.ruiyian.project.log.domain.OperationLog;
import com.ruiyian.project.log.domain.OperationRestore;
import com.ruiyian.project.log.domain.PlanLogMain;
import com.ruiyian.project.log.service.OperationLogService;
import com.ruiyian.project.log.service.OperationRestoreService;
import com.ruiyian.project.log.service.PlanLogMainService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author chong
 * @email 673426164@qq.com
 * @create 2021/1/26
 * Desc: 预案日志控制层
 */
@Slf4j
@RestController
@RequestMapping("/log/planMain")
public class PlanLogMainController extends BaseController {
    @Autowired
    private PlanLogMainService planLogMainService;
    @Autowired
    private OperationLogService operationLogService;
    @Autowired
    private OperationRestoreService operationRestoreService;
    /**
     * 日志列表
     * @param planLogMain
     * @return
     */
    @PostMapping("/list")
    public TableDataInfo list(@RequestBody PlanLogMain planLogMain) {
        startPage();
        List<PlanLogMain> list = planLogMainService.selectList(planLogMain);
        return getDataTable(list);
    }

    /**
     * 保存
     * @param planLogMain
     * @return
     */
    @PostMapping("/save")
    public AjaxResult save(@RequestBody PlanLogMain planLogMain) {
        try {
            planLogMainService.save(planLogMain);
        } catch (Exception e) {
            logger.error(e.getMessage());
            return AjaxResult.error(e.getMessage());
        }
        return AjaxResult.success();
    }

    /**
     * 根据id获取对象
     * @param id
     * @return
     */
    @PostMapping("/findOne")
    public AjaxResult findOne(@RequestBody Long id) {
        PlanLogMain planLogMain = null;
        try {
            planLogMain = planLogMainService.findOne(id);
        } catch (Exception e) {
            logger.error(e.getMessage());
            return AjaxResult.error(e.getMessage());
        }
        return AjaxResult.success(planLogMain);
    }

    /**
     * 操作日志
     * @param operationLog
     * @return
     */
    @PostMapping("/operationList")
    public TableDataInfo operationList(@RequestBody OperationLog operationLog) {
        startPage();
        List<OperationLog> list = operationLogService.selectList(operationLog);
        return getDataTable(list);
    }

    /**
     * 恢复日志
     * @param operationRestore
     * @return
     */
    @PostMapping("/restoreList")
    public TableDataInfo restoreList(@RequestBody OperationRestore operationRestore) {
        startPage();
        List<OperationRestore> list = operationRestoreService.selectList(operationRestore);
        return getDataTable(list);
    }

    /**
     * 预案统计
     * @param planLogMain
     * @return
     */
    @PostMapping("/selectLine")
    public AjaxResult selectLine(@RequestBody PlanLogMain planLogMain) {
        Map<String,Object> data = Maps.newHashMap();
        try {
            List<LineVo> list = planLogMainService.selectLine(planLogMain);
            List<String>  names = list.stream().map(LineVo::getName).collect(Collectors.toList());
            List<String>  values = list.stream().map(LineVo::getValue).collect(Collectors.toList());
            List series = Lists.newArrayList();
            Map map = Maps.newHashMap();
            map.put("data",values);
            map.put("name","执行次数");
            series.add(map);
            data.put("category",names);
            data.put("series",series);
        } catch (Exception e) {
            logger.error(e.getMessage());
            AjaxResult.error(e.getMessage());
        }
        return AjaxResult.success(data);
    }

    /**
     * 查询隧道对应的日志
     * @param planLogMain
     * @return
     */
    @PostMapping("/selectLogList")
    public AjaxResult selectLogList(@RequestBody PlanLogMain planLogMain) {
        List<OperationLog> list = null;
        try {
            list = planLogMainService.selectLogList(planLogMain);
        } catch (Exception e) {
            logger.error(e.getMessage());
            AjaxResult.error(e.getMessage());
        }
        return AjaxResult.success(list);
    }

}
